package com.leezk.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.dto.TagMapperDto;
import com.leezk.order.entity.Dictionary;
import com.leezk.order.entity.Menu;
import com.leezk.order.entity.TagMapper;
import com.leezk.order.exception.NoDataException;
import com.leezk.order.exception.OperateException;
import com.leezk.order.mapper.DictionaryMapper;
import com.leezk.order.mapper.MenuMapper;
import com.leezk.order.mapper.TagMapperMapper;
import com.leezk.order.service.IDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {
    @Autowired
    private  DictionaryMapper dictionaryMapper;

    @Autowired
    private TagMapperMapper tagMapperMapper;

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private StudentLikeService likeService;

    @Override
    public Page<Dictionary> getDictionarys(Page<Dictionary> page) {
        return dictionaryMapper.selectPage(page,null);
    }

    @Override
    public List<Dictionary> getValidDic() {
        List<Dictionary> dictionaryList = this.list();
        List<TagMapper> tagMappers = tagMapperMapper.selectList(null);
        Stream<Dictionary> dictionaryStream = dictionaryList.stream().filter(dictionary -> !exists(tagMappers, dictionary.getId()));
        List<Dictionary> result = dictionaryStream.collect(Collectors.toList());
        if (result.size()==0){
            throw new NoDataException("当前推荐字典已经全部添加，请直接操作推荐内容");
        }
        return result;
    }

    @Override
    public Boolean addDic(Dictionary dictionary) {
        List<Dictionary> dictionaries = dictionaryMapper.selectList(null);
        for (Dictionary dictionary1 : dictionaries) {
            //校验分类code合法性 如果code一样 那么名称必定相同
            if (Objects.equals(dictionary1.getTypeCode(), dictionary.getTypeCode())){
                    dictionary.setTypeName(dictionary1.getTypeName());
            }else{
                //如果code不一样 那么名称一定不能相同
                if (dictionary.getTypeName().equals(dictionary1.getTypeName())){
                    throw new NoDataException("当前推荐字典名称已经被使用，请重新制定");
                }
            }
            if (dictionary.getValueName().equals(dictionary1.getValueName())){
                throw new NoDataException("当前推荐类别已经存在，请重新制定");
            }
        }
        return save(dictionary);
    }

    @Override
    @Transactional
    public Boolean deleteDic(int id) {
        int i = dictionaryMapper.deleteById(id);
        if (i==1){
            tagMapperMapper.delete(new QueryWrapper<TagMapper>().eq("did",id));
        }
        return i==1;
    }

    @Override
    public List<TagMapperDto> getUseDic(int studentId) {
        List<TagMapper> records = tagMapperMapper.selectList(null);
        List<TagMapperDto> tagMapperDtoList=new ArrayList<>();
        TagMapperDto allMenus=new TagMapperDto();
        Dictionary allMenusDic=new Dictionary();
        allMenusDic.setId(0);
        allMenusDic.setValueName("全部菜单");
        allMenus.setDictionary(allMenusDic);
        allMenus.setMenus(menuMapper.selectList(null));

        TagMapperDto likeMenus=new TagMapperDto();
        Dictionary likeMenusDic=new Dictionary();
        likeMenusDic.setId(-1);
        likeMenusDic.setValueName("热门菜单");
        likeMenus.setDictionary(likeMenusDic);
        Set<Integer> menus1 = likeService.getMenus(studentId, 0.5);
        List<Menu> menuList=new ArrayList<>();
        for (Integer integer : menus1) {
            menuList.add(menuMapper.selectById(integer));
        }
        likeMenus.setMenus(menuList);

        tagMapperDtoList.add(allMenus);
        tagMapperDtoList.add(likeMenus);
        for (TagMapper record : records) {
            TagMapperDto mapperDto=new TagMapperDto();
            BeanUtils.copyProperties(record,mapperDto);
            List<Menu> menus=new ArrayList<>();
            if (record.getMid()!=null&& !Objects.equals(record.getMid(), "")){
                String[] split = record.getMid().split(",");
                for (String s : split) {
                    Menu menu = menuMapper.selectById(s);
                    if (menu!=null){
                        menus.add(menu);
                    }

                }
                mapperDto.setMenus(menus);
            }
            mapperDto.setDictionary(dictionaryMapper.selectById(record.getDid()));
            tagMapperDtoList.add(mapperDto);
        }


        return tagMapperDtoList;
    }

    public Boolean exists(List<TagMapper> tagMappers,int id){
        boolean result=false;
        for (TagMapper tagMapper : tagMappers) {
            if (tagMapper.getDid()==id){
                result=true;
                break;
            }
        }
        return result;
    }
}
